<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$company_id = $auth['company_id'];
$user_id = $auth['id'];
if(isset($_POST['addform'])){
    $label = explode(",",implode(",",$_POST['label']));
    $sameLabelCount = 0;
    for($a=0;$a<count($label);$a++){
        $oldLabel = $label[$a];
        for($b=0;$b<count($label);$b++){
            if($oldLabel==$label[$b]){
                $sameLabelCount++;
            }
        }
    }
    if($sameLabelCount==count($label)){
        $workflow_id = $_POST['worflow_id'];
        $options = explode(",",implode(",",$_POST['options']));
        $sft = explode(",",implode(",",$_POST['sft'])); //sft = show field to?(admin or user)
        $fieldsize = explode(",",implode(",",$_POST['fieldsize'])); //field size
        $fieldlayout = explode(",",implode(",",$_POST['fieldlayout'])); //field layout
        $choicesPush = explode(",",implode(",",$_POST['choicesPush']));
        $formHeader = explode("`",$_POST['formHeader']); //form header
        $name_format = explode(",",implode(",",$_POST['name_format']));
        $fof = explode(",",implode(",",$_POST['fof']));
        $ftu = explode(",",implode(",",$_POST['ftu']));
        $objectType_array = explode(",",implode(",",$_POST['objectType_array']));
        $lb_desc_arr = explode(",",implode(",",$_POST['lb_desc_arr']));
        $userForm = explode(",",implode(",",$_POST['userForm']));
        $timezone = "Asia/Manila";
        if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
        $query_form_counter = $db->query("SELECT * from tbform where company_id = '$company_id' and is_active=1","numrows");
        if($query_form_counter>=5){
            echo "forms excess";
        }else{
            //for form header
            $insert_tbform = array(
                "workflow_id"=>$workflow_id,
                "form_title"=>$formHeader[0],
                "description"=>$formHeader[1],
                "label_placement"=>$formHeader[2],
                "company_id"=>$company_id,
                "user_id"=>$user_id,
                "date_done"=>date("Y-m-d  H:i:s"),
                "is_active"=>1,
            );
            $form_id = $db->insert("tbform",$insert_tbform);
            $start_loop = 0;
            //for fields
            for($b=0;$b<count($label);$b++){
                //insert the fields
                $insert_tbfields = array(
                    "form_id"=>$form_id,
                    "fof"=>$fof[$b],
                    "ftu"=>$ftu[$b],
                    "f_label"=>$label[$b],
                    "f_size"=>$fieldsize[$b],
                    "options"=>$options[$b],
                    "sft"=>$sft[$b],
                    "f_layout"=>$fieldlayout[$b],
                    "name_format"=>$name_format[$b],
                    "f_type"=>$objectType_array[$b],
                    "lb_desc"=>$lb_desc_arr[$b]
                );
                $field_id = $db->insert("tbfield_output",$insert_tbfields);
                
                //for choices
                if($choicesPush[$b]!="0"){
                    $choice = substr($choicesPush[$b],0,strlen($choicesPush[$b])-1);
                    $choice = explode("`",$choice);
                    for($d=$start_loop;$d<count($choice);$d++){ //insert choices
                        $choice_check = explode(":",$choice[$d]);
                        $insert_tbchoices = array(
                            "field_id"=>$field_id,
                            "form_id"=>$form_id,
                            "choice_label"=>$choice_check[0],
                            "checked"=>$choice_check[1]
                        );
                        $db->insert("tbchoices",$insert_tbchoices);
                    }
                    $start_loop = count($choice);
                }
            }
            for($u=0;$u<count($userForm);$u++){
                $insert_tbformUser = array(
                    "form_id"=>$form_id,
                    "user"=>$userForm[$u],
                    "is_active"=>1
                );
                $db->insert("tbform_user",$insert_tbformUser);
            }
            echo $form_id;
        }
    }else{
        echo 0;
    }
}elseif(isset($_POST['confAddForm'])){
    $query_form_counter = $db->query("SELECT * from tbform where company_id = '$company_id' and is_active=1","numrows");
    if($query_form_counter>=5){
        echo 0;
    }else{
        echo 1;
        
    }
}

?>